
package com.learnncode.mediachooser.activity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.learnncode.mediachooser.MediaChooser;
import com.learnncode.mediachooser.MediaChooserConstants;
import com.learnncode.mediachooser.R;
import com.learnncode.mediachooser.fragment.ImageFragment;
import com.learnncode.mediachooser.fragment.VideoFragment;

public class HomeAddVideoFragmentActivity extends FragmentActivity implements VideoFragment.OnVideoSelectedListener{

	private FragmentTabHost mTabHost;
	private TextView headerBarTitle;
	private ImageView headerBarImg;
	private ImageView headerBarBack;
	private ImageView headerBarDone;

	private static Uri fileUri;

	private final Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置全屏模式 
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
		WindowManager.LayoutParams.FLAG_FULLSCREEN);  
		 // 去除标题栏 
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_home_media_chooser);

		headerBarTitle  = (TextView)findViewById(R.id.titleTextViewFromMediaChooserHeaderBar);         //标题：应为请选择图片
		headerBarImg = (ImageView)findViewById(R.id.cameraImageViewFromMediaChooserHeaderBar);      //打开拍视频图标
		headerBarBack   = (ImageView)findViewById(R.id.backArrowImageViewFromMediaChooserHeaderView);  //返回箭头
		headerBarDone   = (ImageView)findViewById(R.id.doneImageViewFromMediaChooserHeaderView);       //对号
		mTabHost        = (FragmentTabHost) findViewById(android.R.id.tabhost);

		mTabHost.setup(this, getSupportFragmentManager(), R.id.realTabcontent);
		headerBarBack.setOnClickListener(clickListener);
		headerBarImg.setOnClickListener(clickListener);
		headerBarDone.setOnClickListener(clickListener);

		if(! MediaChooserConstants.showCameraVideo){
			headerBarImg.setVisibility(View.GONE);
		}

		//XYC
		headerBarTitle.setText(getResources().getString(R.string.video));
		headerBarImg.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_video_button));
		headerBarImg.setTag(getResources().getString(R.string.video));

		//判断是否是第一次从添加按钮中启动该activity
		if(getIntent() != null && (getIntent().getBooleanExtra("isFromBucket", false))){

			if(getIntent().getBooleanExtra("image", false)){

			}else{
				headerBarTitle.setText(getResources().getString(R.string.video));
				headerBarImg.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_video_button));
				headerBarImg.setTag(getResources().getString(R.string.video));

				Bundle bundle = new Bundle();
				bundle.putString("name", getIntent().getStringExtra("name"));
				mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(getResources().getString(R.string.folderVidTabTitle) + "      "), VideoFragment.class, bundle);
			}
		}else{

			//XYC重写
			if(MediaChooserConstants.showVideo){
				mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(getResources().getString(R.string.folderPicTabTitle) + "      "), VideoFragment.class, null);
			}

		}

		for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {

			TextView textView = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
			if(textView.getLayoutParams() instanceof RelativeLayout.LayoutParams){

				RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) textView.getLayoutParams();
				params.addRule(RelativeLayout.CENTER_HORIZONTAL);
				params.addRule(RelativeLayout.CENTER_VERTICAL);
				params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
				params.width  = RelativeLayout.LayoutParams.WRAP_CONTENT;
				mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title).setLayoutParams(params);

			}else if(textView.getLayoutParams() instanceof LinearLayout.LayoutParams){
				LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) textView.getLayoutParams();
				params.gravity = Gravity.CENTER;
				mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title).setLayoutParams(params);
			}

		}
		
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {

				android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
				ImageFragment imageFragment  = (ImageFragment) fragmentManager.findFragmentByTag("tab1");
				VideoFragment videoFragment  = (VideoFragment) fragmentManager.findFragmentByTag("tab2");
				android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();


				if(tabId.equalsIgnoreCase("tab1")){
//
//					headerBarTitle.setText(getResources().getString(R.string.image));
//					headerBarImg.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_camera_button));
//					headerBarImg.setTag(getResources().getString(R.string.image));

					if(imageFragment != null){   

						if(videoFragment != null){
							fragmentTransaction.hide(videoFragment);
						}
						fragmentTransaction.show(imageFragment); 
					}
				}else{ 
//					headerBarTitle.setText(getResources().getString(R.string.video));
//					headerBarImg.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_video_button));
//					headerBarImg.setTag(getResources().getString(R.string.video));

					if(videoFragment != null){

						if(imageFragment != null){
							fragmentTransaction.hide(imageFragment);
						}

						fragmentTransaction.show(videoFragment); 
						if(videoFragment.getAdapter() != null){
							videoFragment.getAdapter().notifyDataSetChanged();
						}
					}
				}
				fragmentTransaction.commit();         
			}
		});
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			if(view == headerBarImg){

				if(view.getTag().toString().equals(getResources().getString(R.string.video))){
					Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
					fileUri = getOutputMediaFileUri(MediaChooserConstants.MEDIA_TYPE_VIDEO); // create a file to save the image
					intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

					// start the image capture Intent
					startActivityForResult(intent, MediaChooserConstants.CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);

				}else{
					Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					fileUri = getOutputMediaFileUri(MediaChooserConstants.MEDIA_TYPE_IMAGE); // create a file to save the image
					intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

					// start the image capture Intent
					startActivityForResult(intent, MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
				}
			}else if(view == headerBarDone){

				android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
				//ImageFragment imageFragment = (ImageFragment) fragmentManager.findFragmentByTag("tab1");
				VideoFragment videoFragment = (VideoFragment) fragmentManager.findFragmentByTag("tab1");

				if(videoFragment != null){
					if(videoFragment.getSelectedVideoList() != null && videoFragment.getSelectedVideoList() .size() > 0){
						Intent videoIntent = new Intent();
						videoIntent.setAction(MediaChooser.VIDEO_SELECTED_ACTION_FROM_MEDIA_CHOOSER );
						videoIntent.putStringArrayListExtra("list", videoFragment.getSelectedVideoList());
						sendBroadcast(videoIntent);
					}
					
					finish();
				}else{
					Toast.makeText(HomeAddVideoFragmentActivity.this, getString(R.string.plaese_select_file), Toast.LENGTH_SHORT).show();
				}

			}else if(view == headerBarBack){
				finish();
			}
		}
	};

	/** Create a file Uri for saving an image or video */
	private Uri getOutputMediaFileUri(int type){
		return Uri.fromFile(getOutputMediaFile(type));
	}

	/** Create a File for saving an image or video */
	private static File getOutputMediaFile(int type){

		File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), MediaChooserConstants.folderName);
		if (! mediaStorageDir.exists()){
			if (! mediaStorageDir.mkdirs()){
				return null;
			}
		}

		// Create a media file name
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
		File mediaFile;
		if (type == MediaChooserConstants.MEDIA_TYPE_IMAGE){
			mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
		} else if(type == MediaChooserConstants.MEDIA_TYPE_VIDEO) {
			mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
		} else {
			return null;
		}

		return mediaFile;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == MediaChooserConstants.CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
			if (resultCode == RESULT_OK ) {

				sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri));
				final AlertDialog alertDialog = MediaChooserConstants.getDialog(HomeAddVideoFragmentActivity.this).create();
				alertDialog.show();
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						//Do something after 5000ms
						String fileUriString = fileUri.toString().replaceFirst("file:///", "/").trim();
						android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
						VideoFragment videoFragment = (VideoFragment) fragmentManager.findFragmentByTag("tab2");
						//						
						if(videoFragment == null){   
							VideoFragment newVideoFragment = new VideoFragment();
							newVideoFragment.addItem(fileUriString);

						}else{
							videoFragment.addItem(fileUriString);
						}
						alertDialog.cancel();
					}
				}, 5000);


			} else if (resultCode == RESULT_CANCELED) {
				// User cancelled the video capture
			} else {
				// Video capture failed, advise user
			}
		}else if (requestCode == MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
			if (resultCode == RESULT_OK ) {

				sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri));

				final AlertDialog alertDialog = MediaChooserConstants.getDialog(HomeAddVideoFragmentActivity.this).create();
				alertDialog.show();
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						//Do something after 5000ms
						String fileUriString = fileUri.toString().replaceFirst("file:///", "/").trim();
						android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
						ImageFragment imageFragment = (ImageFragment) fragmentManager.findFragmentByTag("tab1");
						if(imageFragment == null){   
							ImageFragment newImageFragment = new ImageFragment();
							newImageFragment.addItem(fileUriString);

						}else{
							imageFragment.addItem(fileUriString);
						}
						alertDialog.cancel();
					}
				}, 5000);
			} 
		}
	}

	@Override
	public void onVideoSelected(int count){
		if(count != 0){
			((TextView) mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title)).setText(getResources().getString(R.string.videos_tab) + "  "  + count);

		}else{
			((TextView)mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title)).setText(getResources().getString(R.string.videoTabTitle));
		}
	}
}
